<?php
/**
 * Service Unavailable Exception
 *
 * Official HTTP Definition:
 * The server is currently unable to handle the request due to a temporary
 * overloading or maintenance of the server. The implication is that this is a
 * temporary condition which will be alleviated after some delay. If known, the
 * length of the delay MAY be indicated in a Retry-After header. If no
 * Retry-After is given, the client SHOULD handle the response as it would for a
 * 500 response. 
 *
 * @package     Visi
 * @subpackage  Exceptions
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 * @todo        Implement this in the application. Make the app actually return
 *              the correct response headers when this is thrown.
 */
class Visi_Controller_Response_Exception_ServiceUnavailable extends Visi_Controller_Response_Exception {

    public $code = 503;
    
    public $message = 'Service Unavailable';
    
    protected $_retryAfter;
    
    public function __construct($message = null, $code = 0, $retryAfter = null) {
    
        $this->setRetryAfter($retryAfter);
        return parent::__construct($message, $code);
    
    }
    
    /**
     * Set the date/time the client should retry the request. This can either be
     * an amount of seconds or a PHP DateTime object.
     */
    public function setRetryAfter($retryAfter) {
    
        if (!($retryAfter instanceof DateTime)) {
            $retryAfter = (integer) $retryAfter;
            $retryAfter = new DateTime(strtotime("+ {$retryAfter} seconds"));
        }
        $this->_retryAfter = $retryAfter;
        return $this;
    
    }

}